'use strict';

/**
 * The main Sails Angular app module
 *
 * @type {angular.Module}
 */
var app = angular.module('app', ['ui.bootstrap', 'ngFileUpload', 'angular-loading-bar', 'angular-toasty', 'LocalStorageModule']);

app.config(function (localStorageServiceProvider, $httpProvider) {
  localStorageServiceProvider
    .setPrefix('eZjs_');

  $httpProvider.interceptors.push('httpInterceptor');
});

app.factory('httpInterceptor', function($q) {
  return {
    response: function(result){
      if (result.status !== 200) {
        toastr.error('服务器错误');
      }
      return result;
    },
    responseError: function(result){
      toastr.error('服务器错误');
      return $q.reject(result);
    }
  };
});

app.directive('ngEnter', function () {
    return function (scope, element, attrs) {
        element.bind("keydown keypress", function (event) {
            if(event.which === 13) {
                scope.$apply(function (){
                    scope.$eval(attrs.ngEnter);
                });
                event.preventDefault();
            }
        });
    };
});
